package chapter1_3_wait_notify;

/**
 * 当一个线程调用共享对象的wait（）方法被阻塞挂起后，
 * 如果其他线程中断了该线程， 则该线程会抛出InterruptedException 异常并返回。
 * @author huayuhao
 * @date 2020/12/15 15:53
 */
public class WaitNotifyInterrupt {

    static Object object = new Object();

    public static void main(String[] args) throws InterruptedException {

        System.out.println("演示5s之后打断线程,interrupt可以使wait阻塞的线程中断,抛出异常!");

        Thread thread = new Thread(()->{
            try {
                System.out.println("----begin----");
                // 阻塞当前线程
                synchronized (object){
                    object.wait();
                }
                System.out.println("-----end-----");
            }catch (InterruptedException ex){
                ex.printStackTrace();
            }
        });
        thread.start();
        Thread.sleep(5000);
        System.out.println("---开始中断线程thread");
        thread.interrupt();
        System.out.println("---中断线程thread结束");
    }

}
